package com.mt.demo.config;

import com.corundumstudio.socketio.SocketIOServer;
import com.mt.demo.service.impl.GracefulShutdownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class SocketIOServerStarter implements CommandLineRunner {

    @Autowired
    private SocketIOServer socketIOServer;

    @Autowired
    private GracefulShutdownService gracefulShutdownService;

    @Override
    public void run(String... args) throws Exception {
        // 启动SocketIO服务器
        socketIOServer.start();
        System.out.println("SocketIO服务器启动成功，端口: " + socketIOServer.getConfiguration().getPort());

        // 注册优雅关闭钩子
        gracefulShutdownService.handleShutdownSignal();
    }
}
